package com.guanyou;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCommands;
import redis.clients.jedis.JedisPool;

public class RedisUtil {
    private JedisPool jedisPool;
    private JedisCommands jedisCommand;
    private Jedis jedis;
    boolean bUsePool;

    public RedisUtil(JedisPool jedisPool){
        this.jedisPool = jedisPool;
        bUsePool = true;
    }

    public RedisUtil(JedisCommands jedisCommand){
        this.jedisCommand = jedisCommand;
        bUsePool = false;
    }

    public RedisUtil(Jedis jedis){
        this.jedis = jedis;
        bUsePool = false;
    }

    public JedisCommands getJedis(){
        if(bUsePool) return jedisPool.getResource();
        return jedisCommand;
    }

    public void returnJedis(JedisCommands jedis){
        if(bUsePool) {
            ((Jedis)jedis).close();
        }
    }

    public Jedis getRriginalJedis(){
        if(bUsePool) return jedisPool.getResource();
        return jedis;
    }

    public void returnJedis(Jedis jedis){
        if(bUsePool) {
            jedis.close();
        }
    }
}
